import { _decorator, Component, Node, ImageAsset, Texture2D, SpriteFrame, Sprite, Vec3, tween, Tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Utils')
export class Utils {
  public static loadHeadImage(node: Node, base64Image: string) {
    //  console.log('加载头像=1');

    let image = new Image();
    image.onload = function () {
      let img = new ImageAsset(image);
      var texture = new Texture2D();
      texture.image = img;

      let spriteFrame = new SpriteFrame();
      //console.log('加载=',texture);
      spriteFrame.texture = texture;

      let sprite = node.getComponent(Sprite);
      if (!sprite) {
        sprite = node.addComponent(Sprite);
      }
      sprite.spriteFrame = spriteFrame;
      // console.log('加载头像=2');

    }
    image.src = base64Image;

  }

  public static toMin(seconds: number): string {
    if (seconds <= 0) {
      return '0:00'

    }

    let min = Math.floor(seconds / 60); // 计算分钟数
    let second = seconds % 60; // 计算剩余的秒数

    // 将结果转换为字符串，例如："2:03"
    let timeString = `${min}:${second < 10 ? '0' : ''}${second}`;
    // let timeString = min+":"+(second<10?("0"+second):(second))
    //console.log('toMin()'+timeString,seconds);
    return timeString;
  }

  public static scale(node: Node): Tween<Node> {
    let anima = tween(node)
      .to(0.5, { scale: new Vec3(1.2, 1.2, 1.2) }) // 在2秒内缩放到2倍大小，使用缓动效果
      .to(0.5, { scale: new Vec3(1, 1, 1) })// 再缩放到原来的0.5倍
      .union() // 链式调用下一个动作，这里是重复之前的动作以实现循环效果
      .repeatForever();// 重复永远
    return anima;

  }

  public static numK(num: number) {

    return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');

  }

  public static num1000(num: number) {

    if (num >= 100000) {
      let n = Math.floor(num / 1000);

      return this.numK(n) + 'k';
    } else {
      return this.numK(num);
    }

  }

  public static num1000k2(num: number) {

    if (num >= 100000) {
      let n = Math.floor(num / 1000);

      return this.numK2(n) + ';';
    } else {
      return this.numK2(num);
    }

  }
  public static numK2(num: number) {

    return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1:');

  }


  public static getRequest(url: string) {//这个函数，其实就是js的方法，在我的博客中能找到出处的
    //  var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      var strs = str.split("&");
      for (var i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  }

  public static png = "";

  // 方法2：封装通用替换函数
  public static formatString(text: string, params: Record<string, string>) {
    for (const key in params) {
      text = text.replace(new RegExp(`{${key}}`, "g"), params[key]);
    }
    return text;
  }

// 深拷贝函数
public static deepCopy<T>(obj: T): T {
  if (obj === null || typeof obj !== 'object') {
    return obj;
  }

  // 处理日期对象
  if (obj instanceof Date) {
    return new Date(obj.getTime()) as any;
  }

  // 处理数组
  if (Array.isArray(obj)) {
    const copy = [] as any[];
    for (let i = 0, len = obj.length; i < len; i++) {
      copy[i] = this.deepCopy(obj[i]);
    }
    return copy as any;
  }

  // 处理普通对象
  const copy = {} as { [key in keyof T]: T[key] };
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      copy[key] = this.deepCopy(obj[key]);
    }
  }
  return copy;
}

}


